$(function () {
	var app = new Vue({
		el:'#app',
		data:{
			isCollapse: true,
			labelPosition:'left',
			queryIng : false, //正在查询
			queryData:{activeFlag:'1',page:1,rows:10}, //查询条件
			datagrid:[], //数据列表数据
			totalNum:0, //分页控件配置
			data:[],
			defaultCheckedKeys:[],
			addData:{
				classifyName:"",
				classifyId:""
			},
			selectedRow:null,
			isEdit:false,
			rules:{
				title: [{ required: true, message: '该项必填', trigger: 'change' }],
				coverImage: [{ required: true, message: '该项必填', trigger: 'change' }],
				classifyId: [{ required: true, message: '该项必填', trigger: 'change' }],
				type: [{ required: true, message: '该项必填', trigger: 'change' }],
				showStatus: [{ required: true, message: '该项必填', trigger: 'change' }],
			},
			dialogConfig:{
				formLabelWidth:'120px'
				,dialogFormVisible:false
			},
			imageUrl_coverImage:null,
			size:"mini",
			typeOptions:[{label:"网页链接",value:"0"},{label:"自定义",value:"1"}],
			classifyOptions:[]
		},
		methods: {
			 formatType:function(e){
				for(var i=0;i<app.typeOptions.length;i++){
					if(app.typeOptions[i].value==e){
						return app.typeOptions[i].label
					}
				} 
			 },
			 typeChange:function(e){
				  if(e==0){
					  this.rules.subjectUrl=[{ required: true, message: '该项必填', trigger: 'change' }]
					  delete this.rules.content;
				  }else if(e==1){
					  this.rules.content=[{ required: true, message: '该项必填', trigger: 'change' }]
					  delete this.rules.subjectUrl;
				  }
			},
			classifyChange:function(e){
				for(var i=0;i<app.classifyOptions.length;i++){
					if(e == app.classifyOptions[i].id){
						app.addData.classifyName = app.classifyOptions[i].name;
					}
				}
			},
			init:function () {
				var vm = this
				vm.query();
				vm.queryClassify();
			},
			search:function(){
				//点击查询按钮，默认查询第一页。
				this.queryData.page=1;
				this.query();
			},
			queryClassify:function(){
				$.ajax({
					url :staticURL+'/mall/subjectClassify/listAll',
					dataType : 'json',
					success : function(r) {
						app.classifyOptions = r.data;
					}
				});
			},
			query:function () {
				var vm = this
				if(vm.queryIng){
					return;
				}
				vm.queryIng = true;
				$.ajax({
					url :staticURL+'/mall/subject/datagrid',
					data : vm.queryData,
					dataType : 'json',
					success : function(json) {
						app.datagrid = json.rows;
						app.totalNum = json.total;
						app.queryIng = false;
						app.selectedRow = null;
					}
				});
			},
			addItem:function(){
				app.openDialog();
			},
			deleteItem:function(row){
				app.$confirm('是否确认删除操作?', '提示', {
			          confirmButtonText: '确定',
			          cancelButtonText: '取消',
			          type: 'warning'
			        }).then(function () {
			        	$.ajax({
			        		url : staticURL+"/mall/subject/delete?ids="+row.id,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
			        }).catch(function (action) {
			        	  //取消
			        });
				return;
				
			},
			editItem:function(row){
				app.openDialog(row);
			},
			handleSizeChange:function(val){
				app.queryData.rows=val;
				app.query();
			},
			handleCurrentChange:function(val){
				app.queryData.page=val;
				app.query();
			},
			handlePrevClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleNextClick:function(val){
				app.queryData.page=val;
				app.query();
			},
			handleTreeCheckChange:function(data){
				app.queryData.parentCode = data.id;
				app.query();
			},
			resetForm:function(formName) {
				app.$refs[formName].resetFields();
		    },
			submitForm:function(formName){
				app.$refs[formName].validate(function(res){
					app.addData.content=UE.getEditor('add_editor').getContent();
					if(res){
						//验证通过
						$.ajax({
							url : app.saveUrl,
							data : app.addData,
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									app.dialogConfig.dialogFormVisible=false;
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'success',
								          position: 'bottom-right'});
									app.query();
								} else {
									app.$notify({showClose: true,
								          message: r.msg,
								          type: 'error',
								          position: 'bottom-right'});
								}
							}
						});
					}
				});
			},
			openDialog:function(param){
				this.imageUrl_coverImage = null;
				if(param){
					app.isEdit = true;
					//这里如下操作，而不是 app.addData = param,是为了实现JSON对象的复制
					//如果app.addData = param， 当closeDialog()时，dialogForm 被reset
					//app.addDatat同时被reset,会导致bug
					app.addData = JSON.parse(JSON.stringify(param)); 
					
					if(app.addData.coverImage){
						this.imageUrl_coverImage = app.addData.coverImage;
					}
					
				}else{
					app.isEdit = false;
					app.addData = {
							orderNum:1
							,coverImage:""
							,type:"0"
							,showStatus:"1"
							,classifyName:""
							,classifyId:""
							};
				}
				//显示dialog
				app.dialogConfig.dialogFormVisible=true;
				//重置form
				if(app.$refs['dialogForm']){
					app.$refs['dialogForm'].resetFields();
				}
				//设置富文本编辑器
				app.$nextTick(function(){
					UE.getEditor('add_editor');    //添加编辑器
					//加载数据
					UE.getEditor('add_editor').addListener("ready", function () {
						if(app.addData.content){
							// editor准备好之后才可以使用
							UE.getEditor('add_editor').setContent(app.addData.content);
						}
						app.editorReady = true;
					});
					if(app.editorReady && app.addData.content){
						// editor准备好之后才可以使用
						UE.getEditor('add_editor').setContent(app.addData.content);
					}else if(app.editorReady){
						UE.getEditor('add_editor').setContent("");
					}
				})
			},
			closeDialog:function(){
				app.dialogConfig.dialogFormVisible=false;
				app.resetForm('dialogForm')
			},
			handleAvatarSuccess_coverImage:function(res, file) {
				console.log("handleAvatarSuccess_coverImage",res)
				app.addData.coverImage = res.imageUrl;
		        this.imageUrl_coverImage = URL.createObjectURL(file.raw);
		    },
		    uploadUrl:function(){
				
				return staticURL+"/app/mytools/upload";
			},
		    beforeAvatarUpload:function(file) {

		        const isLt2M = file.size / 1024 / 1024 < 2;
	
		        if (!isLt2M) {
		          this.$message.error('上传头像图片大小不能超过 2MB!');
		        }
		        
		        return isLt2M;
		   },
		   datagridSelect:function(row, event, column){
				app.selectedRow = row;
			}
		},
		computed:{
			saveUrl:function(){
				if(app.isEdit){
					return staticURL+'/mall/subject/edit'
				}else{
					return staticURL+'/mall/subject/add'
				}
			},
			columnWidth:function(){

				let w = $(window).width();   

				w = w - 48-48-50-150;
				
				return Math.floor(w/5);
			}
		},
		created: function () {
			this.init();
		},
		updated:function(){
		}
	});
});